package com.galen.service.impl;

import com.galen.dao.UserDao;
import com.galen.entity.User;
import com.galen.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户服务实现
 *
 * @author GalenGao
 * @version Id: UserServiceImpl.java, v 0.1 2022/6/26 12:08 GalenGao Exp $$
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    private UserDao userDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 根据用户名进行查询
        User user = userDao.findByUsername(username);

        if (user == null) {
            throw new UsernameNotFoundException("用户名不存在！");
        }
        return user;
    }

    @Override
    @Transactional
    public UserDetails updatePassword(UserDetails user, String newPassword) {
        String username = user.getUsername();
        User o = userDao.findByUsername(username);
        if (o != null) {
            userDao.updatePassword(newPassword, username);
        }
        return user;
    }
}
